﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Sys_projectpassed : System.Web.UI.Page
{
    PM.PMBusiness.projectapprove project = new PM.PMBusiness.projectapprove();//实例化project
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["tid"] == null || Session["name"] == null || Session["sex"] == null || Session["depart"] == null || Session["usertype"] == null)
        {
            PM.Code.Web.alertmessage.ShowMsg("账号登录已超时，请返回重新登录。", "../Index.aspx");
            return;
        }
        if (!IsPostBack)
        {
            selectbind();
            gridbind();
        }
    }
    //分页绑定
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        gridbind();
    }
    //数据绑定
    protected void gridbind()
    {
        if (ddlPcode.SelectedValue != "")
        {
            StringBuilder sqlstrfield = new StringBuilder();
            StringBuilder sqlstrwhere = new StringBuilder();
            getField(ref sqlstrfield);
            getWhere(ref sqlstrwhere);
            PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "View_Projects", "id", sqlstrfield.ToString(), "id", sqlstrwhere.ToString(), true);
        }
    }
    //设置查询字段
    protected void getField(ref StringBuilder sqlstrfield)
    {
        sqlstrfield.Append("id,pname,type,plevel,pcollege,name,title,attached,case status when 2 then '待终审' when 3 then '待中期检查' when 4 then '待结题' when 5 then '已结题' when 10 then '已取消' when 11 then '延期' end as thestatus");
    }
    //设置查询条件
    protected void getWhere(ref StringBuilder sqlstrwhere)
    {
        sqlstrwhere.Append("1=1");
        //判断用户权限，显示相应数据
        if (Session["usertype"].ToString() == "2")
        {
            sqlstrwhere.Append(" and status in (2,3,4,5,10,11)");
        }
        else if (Session["usertype"].ToString() == "3")
        {
            sqlstrwhere.Append(" and status in (3,4,5,10,11)");
        }
        else
        {
            sqlstrwhere.Append(" and status in (2,3,4,5,10,11)");
        }
        if (ddlDepart.SelectedValue != "")
        {
            sqlstrwhere.AppendFormat(" and pcollege='{0}'", ddlDepart.SelectedItem.Text.ToString());
        }
        if (ddlLevel.SelectedValue != "")
        {
            sqlstrwhere.AppendFormat(" and plevel='{0}'", ddlLevel.SelectedValue.ToString());
        }
        if (ddlType.SelectedValue != "")
        {
            sqlstrwhere.AppendFormat(" and type='{0}'", ddlType.SelectedValue.ToString());
        }
        if (txtName.Text.ToString().Trim() != "")
        {
            sqlstrwhere.AppendFormat(" and name='{0}'", txtName.Text.ToString().Trim());
        }
        if (ddlPcode.SelectedValue != "")
        {
            sqlstrwhere.AppendFormat(" and pcode='{0}'", ddlPcode.SelectedValue.ToString());
        }
        if (txtTopic.Text.ToString().Trim() != "")
        {
            sqlstrwhere.AppendFormat(" and pname like '%{0}%'", txtTopic.Text.ToString().Trim());
        }
    }
    //下拉框数据绑定
    protected void selectbind()
    {
        string str1 = "select collegeid,collegename from Dict_College";
        string str2 = "select btnname from Sys_Guide";
        StringBuilder str3 = new StringBuilder();
        str3.Append("select plancode from Bus_ApprovePlan where opentime<'").Append(DateTime.Now.ToString());
        str3.Append("' and closetime>'").Append(DateTime.Now.ToString()).Append("' and status=1 order by builddate desc");
        PM.Code.Web.controlbind.BindDropDownList(ddlDepart, str1, "collegename", "collegeid");
        PM.Code.Web.controlbind.BindDropDownList(ddlType, str2, "btnname", "btnname");
        PM.Code.Web.controlbind.BindDropDownList(ddlPcode, str3.ToString(), "plancode", "plancode");
        //如果为学院管理员权限，锁定学院下拉框
        if (Session["usertype"].ToString() == "2" && Session["depart"] != null)
        {
            ddlDepart.SelectedIndex = ddlDepart.Items.IndexOf(ddlDepart.Items.FindByText(Session["depart"].ToString().Trim()));
            ddlDepart.Enabled = false;
        }
    }


    protected void btnSeach_Click(object sender, ImageClickEventArgs e)
    {
        gridbind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string pid = e.CommandArgument.ToString().Trim();
        int newstatus = 1;
        int oldstatus = 1;//默认待初审状态
        int theresult = 0;
        int nowstatus = project.checkstatus(int.Parse(pid));

        if (e.CommandName == "Undo")
        {
            if (Session["usertype"].ToString() == "2")
            {
                if (nowstatus==2)
                {
                    oldstatus = 2;//原状态为待终审状态
                    newstatus = 1;//新状态为待初审状态
                }
                else
                {
                    PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "只有状态为“待终审”的项目才能撤销审批！");
                }
            }
            else if (Session["usertype"].ToString() == "3")
            {
                if (nowstatus==3)
                {
                    oldstatus = 3;//原状态为待中期检查状态
                    newstatus = 2;//新状态为待终审状态
                } 
                else
                {
                    PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "只有状态为“待中期检查”的项目才能撤销审批！");
                }
            }
            else
            {
                PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "你没有相应的操作权限，该操作要求必须权限为学院管理员或项目管理员。");
                return;
            }
            theresult = project.projectoperate(oldstatus, newstatus, int.Parse(pid));
            if (theresult == 1)
            {
                gridbind();
            }
            else if (theresult == 99)
            {
                PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "数据库操作出错，请联系管理员。");
            }
            else if (theresult == 0)
            {
                PM.Code.Web.alertmessage.ShowMsg(UpdatePanel1, "操作失败，可能是权限不够或此项目的状态已被他人修改，请刷新页面后再试。");
            }
        }
    }
}
